"""
The entry class for the game. Loads the images and sets up initial objects.
"""
import pygame, sys, ArmyBuilder, random, time, Skills, LocalGame, NetGame
from pygame.locals import *
from Constants import *
from Unit import *
import pygame._view

pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((FIELDWIDTH + INFOWIDTH, FIELDHEIGHT))
pygame.display.set_caption('Nerdscape')
pygame.mouse.set_visible(True)

##########################################################################################
#Important!: The way it's currently coded, images and units must reside at the same index
#in their respective lists
##########################################################################################
#load images
images = [pygame.image.load("images/Megaman.png"),
pygame.image.load("images/Rush.png"),
pygame.image.load("images/Bass.png"),
pygame.image.load("images/Treble.png"),
pygame.image.load("images/Ghost.png"),
pygame.image.load("images/Kirby.png"),
pygame.image.load("images/Jessica.png"),
pygame.image.load("images/Domingo.png"),
pygame.image.load("images/Raziel.png")]

#hard code some units
#               name-hp-move-range-attack-defense-points-skill 
units = [
Unit("Megaman", 5, 10, 3, 3, 1, 100, Skills.Megabuster()),
Unit("Rush", 5, 2, 3, 1, 0, 100),
Unit("Bass",    99, 10, 3, 3, 1, 100, Skills.RapidFire()),
Unit("Treble", 5, 2, 3, 1, 0, 100),
Unit("Ghost",   5, 3, 4, 2, 0, 100, Skills.RailGun()),
Unit("Kirby",   5, 3, 1, 4, 2, 100, Skills.Swallow()),
Unit("Jessica",   5, 10, 4, 2, 0, 100, Skills.HealLitany()),
Unit("Domingo",   5, 10, 1, 4, 2, 100, Skills.Freeze()),
Unit("Raziel",      5, 10, 1, 4, 2, 100, Skills.SoulReaver())]

assets = {"images":images, "units":units, "clock":mainClock}
    
titleFont = pygame.font.SysFont(None, 144)
buttonFont = pygame.font.SysFont(None, 72)
title = titleFont.render("Nerdscape", True, (0,0,0), (200,200,200))
titleRect = title.get_rect()
winRect = windowSurface.get_rect()
titleRect.top = 50
titleRect.centerx = winRect.centerx

startButton = buttonFont.render("Play Game", True, (0,0,0), (200,200,200))
startRect = startButton.get_rect()
quitButton = buttonFont.render("Quit", True, (0,0,0), (200,200,200))
quitRect = quitButton.get_rect()
buildButton = buttonFont.render("Build Army", True, (0,0,0), (200,200,200))
buildRect = buildButton.get_rect()
hostButton = buttonFont.render("Host", True, (0,0,0), (200,200,200))
hostRect = hostButton.get_rect()
joinButton = buttonFont.render("Join", True, (0,0,0), (200,200,200))
joinRect = joinButton.get_rect()

startRect.top = 200
startRect.centerx = winRect.centerx
buildRect.top = 300
buildRect.centerx = winRect.centerx
hostRect.top = 400
hostRect.right = winRect.centerx - 10
joinRect.top = 400
joinRect.left = winRect.centerx + 10
quitRect.top = 500
quitRect.centerx = winRect.centerx


run = True
def draw():
    windowSurface.fill((255,255,255))
    for x in range(0,10):
        for i in assets["images"]:
            windowSurface.blit(i, (random.randint(0,1000),random.randint(0,600)))
    windowSurface.blit(title, titleRect)
    pygame.draw.rect(windowSurface, (0,0,0), titleRect.inflate(6,6), 5)
    windowSurface.blit(startButton, startRect)
    pygame.draw.rect(windowSurface, (0,0,0), startRect, 2)
    windowSurface.blit(buildButton, buildRect)
    pygame.draw.rect(windowSurface, (0,0,0), buildRect, 2)
    windowSurface.blit(quitButton, quitRect)
    pygame.draw.rect(windowSurface, (0,0,0), quitRect, 2)
    windowSurface.blit(hostButton, hostRect)
    pygame.draw.rect(windowSurface, (0,0,0), hostRect, 2)
    windowSurface.blit(joinButton, joinRect)
    pygame.draw.rect(windowSurface, (0,0,0), joinRect, 2)
    pygame.display.update() 

draw()
while run:
    event = pygame.event.wait()
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
    elif event.type == KEYDOWN:
        if event.key == K_F1:
            pygame.image.save(windowSurface, "screenshot"+str(time.time())+".png")
    elif event.type == MOUSEBUTTONDOWN:
        if startRect.collidepoint(event.pos):
            LocalGame.play(windowSurface, assets)
            draw()
        if buildRect.collidepoint(event.pos):
            ArmyBuilder.buildArmy(windowSurface, assets)
            draw()
        if hostRect.collidepoint(event.pos):
            NetGame.play(windowSurface, assets, "host")
            draw()
        if joinRect.collidepoint(event.pos):
            NetGame.play(windowSurface, assets, "client")
            draw()
        if quitRect.collidepoint(event.pos):
            pygame.quit()
            sys.exit()
            
            